package com.luoying.itew.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.luoying.itew.R;
import com.luoying.itew.bean.MonitorBean;
import com.luoying.itew.util.StateContext;

import java.util.List;

/**
 * Created by lufan on 2018/5/31
 * Class describe 监测点列表适配器
 */
public class PointsListAdapter extends RecyclerView.Adapter<PointsListAdapter.PointsViewHodler> {
    View view;
    private List<MonitorBean> list;
    private OnItemClickListener listener;

    public PointsListAdapter(List<MonitorBean> allMonitorList) {
        this.list = allMonitorList;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public PointsViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_points_layout, parent, false);
        return new PointsViewHodler(view);
    }

    @Override
    public void onBindViewHolder(PointsViewHodler holder, final int position) {
        holder.nameTv.setText(list.get(position).getMonitorPointName());

        StateContext state = new StateContext(list.get(position).getMonitorPointStatus(),
                list.get(position).getWarnCoefficient());
        holder.stateTv.setText(state.levelStr());
        holder.stateTv.setTextColor(holder.rootView.getResources().getColor(state.levelColor()));
        holder.rootView.setOnClickListener(v -> {
            if(listener!=null){
                listener.onClick(list.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class PointsViewHodler extends RecyclerView.ViewHolder {
        TextView nameTv;
        TextView stateTv;
        View rootView;

        public PointsViewHodler(View itemView) {
            super(itemView);
            this.rootView = itemView;
            nameTv = (TextView) itemView.findViewById(R.id.points_name_tv);
            stateTv = (TextView) itemView.findViewById(R.id.points_state_tv);

        }
    }

    public interface OnItemClickListener{
        void onClick(MonitorBean bean);
    }
}
